Page({
  data: {
    theme: '',
    path: '',
    newpath: '',
    tpqua: 80,
    tpwid: '',
    tphei: '',
    spgjms: false,
    spqua: '',
    spbit: '',
    spfps: '',
    spres: 0,
    array: ['低', '中', '高'],
    label: '请选择'
  },
  switch(e) {
    this.setData({
      spgjms: e.detail.value
    })
  },
  picker(e) {
    var t = e.detail.value
    if (t == 0) {
      this.setData({
        spqua: 'low',
        label: '低'
      })
    }
    if (t == 1) {
      this.setData({
        spqua: 'medium',
        label: '中'
      })
    }
    if (t == 2) {
      this.setData({
        spqua: 'high',
        label: '高'
      })
    }
  },
  onShow() {
    var that = this
    that.setData({
      theme: wx.getAppBaseInfo().theme
    })
  },
  onShareAppMessage() {
    return {
      title: '压缩图片、视频',
      imageUrl: '../../image/fxfm.png'
    }
  },
  xztp() {
    var that = this
    if (wx.getStorageSync('qxsetting').sytpsp != true) {
      wx.showModal({
        title: '提示',
        content: '“使用选中的图片、视频”权限未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“使用选中的图片、视频”权限。',
        success(res) {
          if (res.confirm) {
            that.router.navigateTo({
              url: '../qxgl/qxgl'
            })
          }
        }
      })
      return
    }
    if (wx.getStorageSync('qxsetting').sytpspa != true) {
      wx.showModal({
        title: '提示',
        content: '“使用选中的图片、视频”权限的“压缩图片、视频”场景未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“使用选中的图片、视频”权限的“压缩图片、视频”场景。',
        success(res) {
          if (res.confirm) {
            that.router.navigateTo({
              url: '../qxgl/qxgl'
            })
          }
        }
      })
      return
    }
    wx.showActionSheet({
      itemList: ['从相册中选择', '从微信聊天中选择'],
      success(res) {
        if (res.tapIndex == 0) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['image'],
            sourceType: ['album'],
            sizeType: ['original'],
            success(res) {
              that.setData({
                path: res.tempFiles[0].tempFilePath
              })
              that.ystp()
            }
          })
        }
        if (res.tapIndex == 1) {
          wx.chooseMessageFile({
            count: 1,
            type: 'image',
            success(res) {
              that.setData({
                path: res.tempFiles[0].path
              })
              that.ystp()
            },
            fail() {
              wx.showToast({
                title: '暂不支持',
                icon: 'error'
              })
            }
          })
        }
      }
    })
  },
  xzsp() {
    var that = this
    if (wx.getStorageSync('qxsetting').sytpsp != true) {
      wx.showModal({
        title: '提示',
        content: '“使用选中的图片、视频”权限未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“使用选中的图片、视频”权限。',
        success(res) {
          if (res.confirm) {
            that.router.navigateTo({
              url: '../qxgl/qxgl'
            })
          }
        }
      })
      return
    }
    if (wx.getStorageSync('qxsetting').sytpspa != true) {
      wx.showModal({
        title: '提示',
        content: '“使用选中的图片、视频”权限的“压缩图片、视频”场景未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“使用选中的图片、视频”权限的“压缩图片、视频”场景。',
        success(res) {
          if (res.confirm) {
            that.router.navigateTo({
              url: '../qxgl/qxgl'
            })
          }
        }
      })
      return
    }
    wx.showActionSheet({
      itemList: ['从相册中选择', '从微信聊天中选择'],
      success(res) {
        if (res.tapIndex == 0) {
          wx.chooseMedia({
            count: 1,
            mediaType: ['video'],
            sourceType: ['album'],
            sizeType: ['original'],
            success(res) {
              that.setData({
                path: res.tempFiles[0].tempFilePath
              })
              that.yssp()
            }
          })
        }
        if (res.tapIndex == 1) {
          wx.chooseMessageFile({
            count: 1,
            type: 'video',
            success(res) {
              that.setData({
                path: res.tempFiles[0].path
              })
              that.yssp()
            },
            fail() {
              wx.showToast({
                title: '暂不支持',
                icon: 'error'
              })
            }
          })
        }
      }
    })
  },
  bctp() {
    var that = this
    wx.showActionSheet({
      itemList: ['转发到聊天', '保存到相册'],
      success(t) {
        if (t.tapIndex == 0) {
          wx.showShareImageMenu({
            path: that.data.newpath,
            style: 'v2',
            needShowEntrance: false
          })
        }
        if (t.tapIndex == 1) {
          if (wx.getStorageSync('qxsetting').bctpspa != true) {
            wx.showModal({
              title: '提示',
              content: '“保存图片、视频到相册”权限的“压缩图片、视频”场景未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“保存图片、视频到相册”权限的“压缩图片、视频”场景。',
              success(res) {
                if (res.confirm) {
                  that.router.navigateTo({
                    url: '../qxgl/qxgl'
                  })
                }
              }
            })
            return
          }
          wx.saveImageToPhotosAlbum({
            filePath: that.data.newpath,
            success() {
              wx.showToast({
                title: '保存成功'
              })
            },
            fail() {
              wx.showModal({
                title: '提示',
                content: '“保存图片、视频到相册”权限未开启，无法使用此功能。如需使用此功能，请点击“确定”进入小程序设置页面开启“添加到相册”权限。',
                success(res) {
                  if (res.confirm) {
                    var app = getApp()
                    if (app.globalData.network == 'none') {
                      wx.showToast({
                        title: '无网络',
                        icon: 'error'
                      })
                      return
                    }
                    wx.openSetting({
                      fail() {
                        wx.showToast({
                          title: '暂不支持',
                          icon: 'error'
                        })
                      }
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  bcsp() {
    var that = this
    wx.showActionSheet({
      itemList: ['转发到聊天', '保存到相册'],
      success(t) {
        if (t.tapIndex == 0) {
          wx.shareVideoMessage({
            videoPath: that.data.newpath
          })
        }
        if (t.tapIndex == 1) {
          if (wx.getStorageSync('qxsetting').bctpspa != true) {
            wx.showModal({
              title: '提示',
              content: '“保存图片、视频到相册”权限的“压缩图片、视频”场景未开启，无法使用此功能。如需使用此功能，请点击“确定”进入权限管理页面开启“保存图片、视频到相册”权限的“压缩图片、视频”场景。',
              success(res) {
                if (res.confirm) {
                  that.router.navigateTo({
                    url: '../qxgl/qxgl'
                  })
                }
              }
            })
            return
          }
          wx.saveVideoToPhotosAlbum({
            filePath: that.data.newpath,
            success() {
              wx.showToast({
                title: '保存成功'
              })
            },
            fail() {
              wx.showModal({
                title: '提示',
                content: '“保存图片、视频到相册”权限未开启，无法使用此功能。如需使用此功能，请点击“确定”进入小程序设置页面开启“添加到相册”权限。',
                success(res) {
                  if (res.confirm) {
                    var app = getApp()
                    if (app.globalData.network == 'none') {
                      wx.showToast({
                        title: '无网络',
                        icon: 'error'
                      })
                      return
                    }
                    wx.openSetting()
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  ystp() {
    var that = this, t = this.data;
    if (t.tpqua == 0) {
      wx.showToast({
        title: '压缩质量不能为0',
        icon: 'none'
      })
      return
    }
    wx.showLoading({
      title: '压缩中',
      mask: true
    })
    if (t.tpwid == '' && t.tphei == '') {
      wx.compressImage({
        src: t.path,
        quality: t.tpqua,
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bctp()
        }
      })
    }
    if (t.tpwid != '' && t.tphei == '') {
      wx.compressImage({
        src: t.path,
        quality: t.tpqua,
        compressedWidth: Number(t.tpwid),
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bctp()
        }
      })
    }
    if (t.tphei != '' && t.tpwid == '') {
      wx.compressImage({
        src: t.path,
        quality: t.tpqua,
        compressedHeight: Number(t.tphei),
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bctp()
        }
      })
    }
    if (t.tpwid != '' && t.tphei != '') {
      wx.compressImage({
        src: t.path,
        quality: t.tpqua,
        compressedWidth: Number(t.tpwid),
        compressedHeight: Number(t.tphei),
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bctp()
        }
      })
    }
  },
  yssp() {
    var that = this, t = this.data;
    if (t.spgjms == false) {
      if (t.spqua == '') {
        wx.showToast({
          title: '请选择压缩质量',
          icon: 'error'
        })
        return
      }
      wx.showLoading({
        title: '压缩中',
        mask: true
      })
      wx.compressVideo({
        src: t.path,
        quality: t.spqua,
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bcsp()
        },
        fail() {
          wx.hideLoading()
          wx.showToast({
            title: '暂不支持',
            icon: 'error'
          })
        }
      })
    }
    if (t.spgjms == true) {
      if (t.spbit == '') {
        wx.showToast({
          title: '请填写压缩后码率',
          icon: 'none'
        })
        return
      }
      if (t.spfps == '') {
        wx.showToast({
          title: '请填写压缩后帧率',
          icon: 'none'
        })
        return
      }
      if (t.spres == 0) {
        wx.showToast({
          title: '相对于原视频的分辨率比例不能为0',
          icon: 'none'
        })
        return
      }
      wx.showLoading({
        title: '压缩中',
        mask: true
      })
      wx.compressVideo({
        src: t.path,
        bitrate: Number(t.spbit),
        fps: Number(t.spfps),
        resolution: t.spres,
        success(res) {
          wx.hideLoading()
          that.setData({
            newpath: res.tempFilePath
          })
          that.bcsp()
        },
        fail() {
          wx.hideLoading()
          wx.showToast({
            title: '暂不支持',
            icon: 'error'
          })
        }
      })
    }
  }
})